博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android不同版本下Notification创建方法
阅读量:6672 次
发布时间:2019-06-25

本文共 4532 字,大约阅读时间需要 15 分钟。

项目环境

Project Build Target:Android 6.0

 

问题:

使用 new Notification(int icon, CharSequence tickerText, long when)构造函数时,Eclipse却提示:" The constructor Notification(int, CharSequence, long) is deprecated "

源码如下:

1 /** 2 * Constructs a Notification object with the information needed to 3 * have a status bar icon without the standard expanded view. 4 * 5 * @param icon The resource id of the icon to put in the status bar. 6 * @param tickerText The text that flows by in the status bar when the notification first 7 * activates. 8 * @param when The time to show in the time field. In the System.currentTimeMillis 9 * timebase.10 *11 * @deprecated Use {
@link Builder} instead.12 */13 @Deprecated14 public Notification(int icon, CharSequence tickerText, long when)15 {16   this.icon = icon;17   this.tickerText = tickerText;18   this.when = when;19 }

 

 

 

  在不同的版本下Notification使用有一些不同,涉及到Builder的使用。现在总结如下,希望对以后使用的程序员有所帮助。

 

  低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

1 Intent  intent = new Intent(this,MainActivity);  2 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  3 notification.setLatestEventInfo(context, title, message, pendingIntent);          4 manager.notify(id, notification);

 

 

 

  高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。

1 Notification.Builder builder = new Notification.Builder(context)  2             .setAutoCancel(true)  3             .setContentTitle("title")  4             .setContentText("describe")  5             .setContentIntent(pendingIntent)  6             .setSmallIcon(R.drawable.ic_launcher)  7             .setWhen(System.currentTimeMillis())  8             .setOngoing(true);  9 notification=builder.getNotification();

 

 

 

  高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。

1 Notification notification = new Notification.Builder(context)    2          .setAutoCancel(true)    3          .setContentTitle("title")    4          .setContentText("describe")    5          .setContentIntent(pendingIntent)    6          .setSmallIcon(R.drawable.ic_launcher)    7          .setWhen(System.currentTimeMillis())    8          .build();

 

 

 

    【注意点】:

    在构造notification的时候有很多种写法,但是要注意,用
  Notification notification = new Notification();
  这种构建方法的时候,一定要加上notification.icon这个设置,不然,程序虽然不会报错,但是会没有效果。 

问题:

使用了Notification下的setLatestEventInfo()方法时,Eclipse却提示:“ The method setLatestEventInfo(Context, String, String, PendingIntent) is undefined for the type Notification”!

查看源码:

1 /** 2 * Sets the {
@link #contentView} field to be a view with the standard "Latest Event" 3 * layout. 4 * 5 *

Uses the {

@link #icon} and {
@link #when} fields to set the icon and time fields 6 * in the view.

7 * @param context The context for your application / activity. 8 * @param contentTitle The title that goes in the expanded entry. 9 * @param contentText The text that goes in the expanded entry.10 * @param contentIntent The intent to launch when the user clicks the expanded notification.11 * If this is an activity, it must include the12 * {
@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires13 * that you take care of task management as described in the14 * Tasks and Back15 * Stack document.16 *17 * @deprecated Use {
@link Builder} instead.18 * @removed19 */20 @Deprecated21 22 public void setLatestEventInfo(Context context,23 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {24 Notification.Builder builder = new Notification.Builder(context);25 26 // First, ensure that key pieces of information that may have been set directly27 // are preserved28 builder.setWhen(this.when);29 builder.setSmallIcon(this.icon);30 builder.setPriority(this.priority);31 builder.setTicker(this.tickerText);32 builder.setNumber(this.number);33 builder.setColor(this.color);34 builder.mFlags = this.flags;35 builder.setSound(this.sound, this.audioStreamType);36 builder.setDefaults(this.defaults);37 builder.setVibrate(this.vibrate);38 builder.setDeleteIntent(this.deleteIntent);39 40 // now apply the latestEventInfo fields41 if (contentTitle != null) {42 builder.setContentTitle(contentTitle);43 }44 if (contentText != null) {45 builder.setContentText(contentText);46 }47 builder.setContentIntent(contentIntent);48 builder.buildInto(this);49 }

 

 

 

setLatestEventInfo方法已被removed。

转载于:https://www.cnblogs.com/arture/p/5523695.html

你可能感兴趣的文章
Out of memory: Kill process 内存不足
查看>>
linux 基础(一)
查看>>
ejb-jar.xml
查看>>
最新杭州地铁开通时间表
查看>>
关闭shift中英文切换 英文代码/中文注释随意切换着写。
查看>>
距离北京奥运还有359天,发布WPF版本的北京2008标志(下)
查看>>
命令模式
查看>>
如何自定义长连接策略
查看>>
平衡二叉树与自平衡二叉树(红黑树)的区别
查看>>
读取JPG图片的Exif属性(一) - Exif信息简介
查看>>
【译文】追求生产极简主义
查看>>
FTP服务器原理(转)
查看>>
精选30道Java笔试题解答
查看>>
特殊符号 UNICODE编码
查看>>
C#图解教程 第八章 表达式和运算符
查看>>
解决NavicatPremium导入CSV文件中文乱码的问题
查看>>
Python基础:语法基础(3)
查看>>
单元测试利器 JUnit 4
查看>>
杭电2097
查看>>
Set default value for column[转]
查看>>